<?php

namespace App\Models;

use App\Service\Base\Model;

class SiteTag extends Model
{

    // 专栏作家
    protected $table = 'site_tag';

    // 缓存字段，用于搜索的字段
    protected $cacheFields = ['id',];

    // {"id":"编号","rank":"排序","en_title":"标签名称（英文）","cn_title":"标签名称（中文）","tc_title":"标签名称（台湾）","number":"数量","is_del":"是否删除（1-是）","ctime":"创建时间","mtime":"修改时间"}


    //[ 'id', 'rank', 'en_title', 'cn_title', 'tc_title', 'number', 'is_del', 'ctime', 'mtime',]


    //array ( 'id' => '$this->getData("id", null, "trim")', 'rank' => '$this->getData("rank", null, "trim")', 'en_title' => '$this->getData("en_title", null, "trim")', 'cn_title' => '$this->getData("cn_title", null, "trim")', 'tc_title' => '$this->getData("tc_title", null, "trim")', 'number' => '$this->getData("number", null, "trim")', 'is_del' => '$this->getData("is_del", null, "trim")', 'ctime' => '$this->getData("ctime", null, "trim")', 'mtime' => '$this->getData("mtime", null, "trim")', )


    //$id = $this->getData("id", null, 'trim'); $rank = $this->getData("rank", null, 'trim'); $en_title = $this->getData("en_title", null, 'trim'); $cn_title = $this->getData("cn_title", null, 'trim'); $tc_title = $this->getData("tc_title", null, 'trim'); $number = $this->getData("number", null, 'trim'); $is_del = $this->getData("is_del", null, 'trim'); $ctime = $this->getData("ctime", null, 'trim'); $mtime = $this->getData("mtime", null, 'trim');


    // 获取所有的话题信息
    public function getAll($where = [], $order = 'rank ASC')
    {
        // 获取缓存
        $bang     = $this->bang;
        $is_cache = $this->is_cache || $bang;
        $args     = compact('where');
        if($is_cache){
            $cacheName = $this->cacheKey('lists', $args);
            $cacheData = $this->cacheClass()->getCache($cacheName);
            if(!empty($cacheData)){
                // 检查是否已经绑定
                $this->checkBangList($cacheData, $cacheName);

                return $cacheData;
            }
        }

        // 查询
        if(is_array($order)){
        } else{
            $order = explode(" ", $order);
        }

        $lists = $this->where($where)->orderby($order[0], $order[1])->get()->toArray();
        //$lists = array_column($lists, null, 'id');
        // 保存为缓存
        if($is_cache){
            $this->addCacheList($lists, $cacheName, $lists, 'list');
        }

        return $lists;
    }

    // 定义验证操作
    public function rules()
    {

        $add = [
        ];

        $edit = [
        ];

        $info = [
        ];


        $rule = [
            //'id'       => 'required',
            //'en_title' => 'required',
            //'cn_title' => 'required',
            //'tc_title' => 'required',
            'rank'   => 'numeric',
            'status' => ['in:0,1,-1'],
            'number' => 'numeric',
            //'ctime'    => 'required',
            //'mtime'    => 'required',
        ];

        $msg = [
            'id'       => '编号',
            'rank'     => '排序',
            'en_title' => '标签名称（英文）',
            'cn_title' => '标签名称（中文）',
            'tc_title' => '标签名称（台湾）',
            'number'   => '数量',
            'status'   => '状态（1-是）',
            'ctime'    => '创建时间',
            'mtime'    => '修改时间',
        ];

        return ['rule' => $rule, 'msg' => $msg, 'add' => $add];
    }
}
